<?php
header ('Content-Type: text/html; charset=utf-8');

class WikipediaRequest
{
	public $title;
	public $urlPage;

	public function __construct() { }

	public function getContent($obj)
	{
		do{
			$datas = $this->getContentPage();
			$datas = utf8_decode($datas);
			$datas = str_replace("&#8216;", "'", $datas);
			$datas = str_replace("&#8217;", "'",$datas);
			$datas = str_replace("&#8230;", "...", $datas);
			$datas = str_replace("&#339;", "oe", $datas);
			$datas = str_replace("&#140;", "OE", $datas);

			//Convert special chars (except <, >, &, " and ') in their html entitie correspondance
			//>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			//>>>>>>>>>>>>>>>>ENCODING PROBLEM<<<<<<<<<<<<<<<<<<<<<<
			//>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<
			/*$trans		= get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES);
			//print_r($trans);
			$trans["<"]	= "<";
			$trans[">"]	= ">";
			$trans["&"]	= "&";
			$datas		= strtr($datas, $trans);*/

			//Get only <body> content
			preg_match("#(< ?body[^>]+>)(.*)(< ?\/ ?body ?>)#is", $datas, $matches);
			//Delete < script >
			$content = preg_replace("#(.?)(\< ?script[^>]+>)(.*?)(< ?\/ ?script ?>)(.?)#is", "\\1\\5", $matches[2]);
			//preg_replace("#(.*)(<[^>]+>)(.*)#is", "\\1", $res);
			//Get title
			preg_match("#<[^>]*firstHeading[^>]*>([^>]*)<\/h1>#is", $content, $matches);
			$this->title = $matches[1];
			//Delete links (not their contents)
			$content = preg_replace("#(.?)< ?a ?[^>]+>(.*?)< ?\/ ?a ?>(.?)#is", "\\1\\2\\3", $content);
			//Delete spans (not their contents)
			$content = preg_replace("#(.?)< ?span ?[^>]+>(.*?)< ?\/ ?span ?>(.?)#is", "\\1\\2\\3", $content);
			//Delete all tables
			$content = preg_replace("#(.?)< ?table ?[^>]+>.*?< ?\/ ?table ?>(.?)#is", "\\1\\2", $content);
			//Delete <dl>
			$content = preg_replace("#(.?)< ?dl ?>.*?< ?\/ ?dl ?>(.?)#is", "\\1\\2", $content);
			//Delete images
			$content = preg_replace("#(.?)< ?img ?[^>]+>(.?)#is", "\\1\\2", $content);
			//Delete title
			$content = preg_replace("#<[^>]*firstHeading[^>]*>([^>]*)<\/h1>#is", "", $content);
			//Delete table rests in case of table imbrication
			$content = preg_replace("#(.?)< ?\/ ?td ?>.*< ?\/ ?table ?>(.*)#is", "\\1\\2", $content);
			//Delete all divs and their contents
			$content = preg_replace("#< ?div ?[^>]*>.*?< ?\/ ?div ?[^>]*>#is", "", $content);
			//<h(x)> replacement
			//Doesn't function
			/*$res = preg_replace("#(.?)<h2>(.*)</h2>(.*)#is", "\\1<font size=\"9\" color=\"red\">\\2</font>\\3", $res);*/

			/*$res = preg_replace("#(.?)\< ?ul ?>(.*?)< ?\/ ?ul ?>(.?)#is", "\\1\\2\\3", $res);
			$res = preg_replace("#(.?)\< ?ol ?>(.*?)< ?\/ ?ol ?>(.?)#is", "\\1\\2\\3", $res);*/

			//Delete </div> that rest
			$content = ereg_replace("< ?\/ ?div ?\>", "", $content);
			//Delete footer
			$content = ereg_replace("<!-- \\\nPre-expand.*", "", $content);
			//delete <hr>
			$content = ereg_replace("< ?hr[^>]+>", "", $content);
			//Delete params
			$content = ereg_replace(" ?id=\"[^\"]*\"", "", $content);
			$content = ereg_replace(" ?class=\"[^\"]*\"", "", $content);
			$content = ereg_replace(" ?style=\"[^\"]*\"", "", $content);
			//Delete comments
			$content = preg_replace("#<!--[^>]*-->#is", "", $content);
			//Delete [xxx]
			$content = preg_replace("#\[[^\]]*\]#is", "", $content);
			$content = preg_replace("#<p></p>#is", "<br/>", $content);
			$content = preg_replace("#<p><br ?/></p>#is", "<br />", $content);

			for($i=1; $i<5; $i++){
				do{
					$content = preg_replace("#<h".$i.">[^>]*?</h".$i.">[^>\w]*?(?:<br />)?[^>\w]*?<br ?/>[^\w]*?<h([0-9])>#is", "<h\\1>", $content);
				}while(preg_match("#<h".$i.">[^>]*?</h".$i.">[^>\w]*?(?:<br />)?[^>\w]*?<br ?/>[^\w]*?<h([0-9])>#is", $content));
			}

			//Replace all unknown chars displayed with ? and placed between two numerals / alphabetical characters by '
			$content = preg_replace("#([\w])\?([\w])#is","\\1'\\2", $content);

			//Replace <h(x)> by <font size="x">
			for($i=0;$i<7; $i++){
				$content = ereg_replace("< ?h".$i." ?>", "<br /><font size=\"".($obj["size"]-$i*2)."\"><b>", $content);
			}
			$content = eregi_replace("< ?\/ ?h[0-9] ?>", "</b></font>", $content);
			//$content = eregi_replace("< ?/p>[^\w]{0,4}<br ?/>", "</p>", $content);

			//Add whites spaces after break lines for the flash textfield cut.
			$content = ereg_replace("(\r|\n){1,}", " ", $content);
			$content = str_replace("\t", " ", $content);
			$content = str_replace("</font> <p>", "</font><br/> <p>", $content);
			do{
				$content = eregi_replace("<br ?/>[^\w]{0,4}<br ?/>", "<br />", $content);
			}while(ereg("<br ?/>[^\w]{0,4}<br ?/>", $content));

		}while(strlen(trim($content))<200);

		$res				= array();
		$res["title"]		= $this->title;
		$res["content"]	= "<font size=\"".($obj["size"]*0.6)."\">".trim($content)."</font>";
		$res["url"]		= $this->urlPage;
		return $res;
	}



	/********************
	** PRIVATE METHODS **
	********************/

	/** Get content of a wikipedia page **/
	private function getContentPage()
	{
		$ch			= curl_init();
		//curl_setopt	($ch, CURLOPT_URL, "http://fr.wikipedia.org/wiki/Varicelle");
		//curl_setopt	($ch, CURLOPT_URL, "http://fr.wikipedia.org/wiki/Ludwig_van_Beethoven");
		curl_setopt	($ch, CURLOPT_URL, "http://fr.wikipedia.org/wiki/Special:Random");
		//curl_setopt	($ch, CURLOPT_URL, "http://localhost/logout/bordel/wikipedia/ressources/page".rand(1,20).".htm");
		//curl_setopt	($ch, CURLOPT_URL, "http://localhost/Flex%20Project/Log_Out/ressources_wikipedia/page".rand(1,20).".htm");
		//curl_setopt	($ch, CURLOPT_URL, "http://localhost/logout/bordel/wikipedia/ressources/page1.htm");
		curl_setopt	($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.2a) Gecko/20021021");
		curl_setopt	($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt	($ch, CURLOPT_FOLLOWLOCATION, 1);
		$this->urlPage	= curl_getinfo($ch ,CURLINFO_EFFECTIVE_URL);
		$res			= curl_exec($ch);
		curl_close($ch);
		return $res;
	}
}
/*$truc = new WikipediaRequest();
$content = $truc->getContent();
echo $content["content"];*/

?>